#include "SpriteText.h"

SpriteText::SpriteText()
: mFont(nullptr)
, mColor(0)
, mPolice(nullptr)
, mMessage(nullptr)
{
	
}

SpriteText::SpriteText(const std::string& police, const std::string& message)
: mPolice(police)
, mMessage(message)
, x(0.0f)
, y(0.0f)
, width(40)
, height(80)
, isItalic(false)
{
	mColor = D3DCOLOR_XRGB(0, 0, 0);
	CreateMyFont();
}

SpriteText::SpriteText(const std::string& police, const std::string& message, float x, float y)
: mPolice(police)
, mMessage(message)
{
	SetPosition(x, y);
	CreateMyFont();
}

SpriteText::SpriteText(const std::string& police, const std::string& message, D3DXVECTOR3* position)
: mPolice(police)
, mMessage(message)
{
	SetPosition(position);
	CreateMyFont();
}

SpriteText::SpriteText(const std::string& police, const std::string& message, int width, int height)
: mPolice(police)
, mMessage(message)
, width(width)
, height(height)
{
	CreateMyFont();
}

SpriteText::SpriteText(const std::string& police, const std::string& message, int width, int height, float x, float y)
: mPolice(police)
, mMessage(message)
, width(width)
, height(height)
{
	SetPosition(x, y);
	CreateMyFont();
}

SpriteText::SpriteText(const std::string& police, const std::string& message, int width, int height, D3DXVECTOR3* position)
: mPolice(police)
, mMessage(message)
, width(width)
, height(height)
{
	SetPosition(position);
	CreateMyFont();
}

SpriteText::SpriteText(const std::string& police, const std::string& message, int width, int height, D3DCOLOR color)
: mPolice(police)
, mMessage(message)
, width(width)
, height(height)
{
	SetColor(color);
	CreateMyFont();
}

SpriteText::SpriteText(const std::string& police, const std::string& message, int width, int height, D3DCOLOR color, float x, float y)
: mPolice(police)
, mMessage(message)
, width(width)
, height(height)
{
	SetPosition(x, y);
	SetColor(color);
	CreateMyFont();
}

SpriteText::SpriteText(const std::string& police, const std::string& message, int width, int height, D3DCOLOR color, D3DXVECTOR3* position)
: mPolice(police)
, mMessage(message)
, width(width)
, height(height)
{
	SetPosition(position);
	SetColor(color);
	CreateMyFont();
}

void SpriteText::CreateMyFont()
{
	D3DXFONT_DESC fontDesc;
	fontDesc.Width = width;
	fontDesc.Height = height;
	fontDesc.MipLevels = 0;
	fontDesc.Italic = isItalic;
	fontDesc.CharSet = DEFAULT_CHARSET;
	fontDesc.OutputPrecision = OUT_DEFAULT_PRECIS;
	fontDesc.Quality = DEFAULT_QUALITY;
	fontDesc.PitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;

	strcpy_s(fontDesc.FaceName, _T(mPolice.c_str()));
	HR(D3DXCreateFontIndirect(gD3DDevice, &fontDesc, &mFont));
}

SpriteText::~SpriteText()
{
	ReleaseCOM(mFont);
	mPolice = nullptr;
	mMessage = nullptr;
}

void SpriteText::OnLostDevice()
{
	mFont->OnLostDevice();
}

void SpriteText::OnResetDevice()
{
	mFont->OnResetDevice();
}

void SpriteText::Update(float dt)
{

}

void SpriteText::Draw(ID3DXSprite* sprite, D3DXMATRIX& view, D3DXMATRIX& proj)
{
	RECT fontRect;
	fontRect = GetTextRect();
	CenterRect(&fontRect);
	MoveToPosition(&fontRect);

	if (mFont)
	{
		mFont->DrawText(0, _T(mMessage.c_str()), -1, &fontRect, DT_CENTER | DT_VCENTER,
			mColor);
	}
}

RECT SpriteText::GetTextRect()
{
	RECT fontRect = { 0, 0, 0, 0 };

	if (mFont)
	{
		mFont->DrawText(0, _T(mMessage.c_str()), -1, &fontRect, DT_CALCRECT,
			D3DCOLOR_XRGB(0, 0, 0));
	}

	return fontRect;
}

void SpriteText::CenterRect(RECT* toCenter)
{
	RECT winRect;
	::GetClientRect(gD3DApp->GetMainWindow(), &winRect);
	int hOffset = ((winRect.right - winRect.left) / 2) - ((toCenter->right - toCenter->left) / 2);
	int vOffset = ((winRect.bottom - winRect.top) / 2) - ((toCenter->bottom - toCenter->top) / 2);
	toCenter->bottom += vOffset;
	toCenter->top += vOffset;
	toCenter->left += hOffset;
	toCenter->right += hOffset;
}

void SpriteText::MoveToPosition(RECT* toPlace)
{
	toPlace->left += (LONG) x;
	toPlace->right += (LONG) x;
	toPlace->bottom += (LONG) y;
	toPlace->top += (LONG) y;
}

